<?php
/**
 * Created by Creatint.
 * Date: 2016/7/19
 * PHP version: +7.0
 */

namespace Admin\Controller;
use Think\Controller;
use Admin\Tool\Wechat;
use Admin\Tool\WechatException;

class WechatController extends Controller{
    protected $officialAccount;//当前公众号数组
    protected $wechatTool;//微信公众号工具类
    protected $receive;//接收到的xml转换为数组

    /**
     * 初始化方法，根据api名称确定公众号
     */
    protected function _initialize(){
        try{

            $api = I("get.api",'');//获取api名称
            if(preg_match('/^[a-zA-Z][a-zA-Z0-9]{1,19}$/',$api)){//过滤api名称
                //echo '匹配';
                $this->officialAccount = D("Wechat")->get_wechat($api);
                //dump($this->officialAccount);
                file_put_contents('/var/www/wechat/flag.log',date("Y-m-d H:i:s")."： \n ".__LINE__."\n ".__FILE__." \n\n",FILE_APPEND);

                $this->wechatTool = new Wechat($this->officialAccount);
            }else{
                //echo  '不匹配';
            }
            if(isset($_GET['echostr'])){
                if($this->wechatTool->checkSignagure()){
                    echo I("get.echostr");
                    D("Wechat")->where("id={$this->officialAccount['id']}")->save(array('status'=>1));
                    exit;//必须
                }
            }
        }catch(WechatException $e){
            echo $e->getCode();
        }
    }


    /**
     *微信API入口  0
     * 根据API接口名称-标识，确定公众号
     */
    public function index(){
        try{
            switch ($this->wechatTool->getWechatMessage()->getMessageType()){
                case 'text':
                    //echo $this->wechatTool->text()->replay();
                    echo
                    <<< 'EFO'
                                    <xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[DEFAULT]]></Content>
</xml>
EFO;
                    break;
                case 'image':
                    //保存
                    break;
                case 'location':
                    //位置
                    break;
                case 'link':
                    //链接
                    break;
                case 'event':
                    //事件
                    break;
                case 'voice':
                    //声音
                    break;
                case 'video':
                    //视频
                    break;
                case 'news':
                    //新闻
                    break;
                default:
                    echo
                    <<< 'EFO'
                                    <xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[DEFAULT]]></Content>
</xml>
EFO;
            }
            //echo 'index';
            //$this->wechatTool->http();
        }catch(WechatException $e){
            echo $e->getCode();
        }
        exit();
    }
}